package test;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import Application.Components.Schedule.Lesson;

/**
 * 
 * @author vit.terl
 *
 */
public class DataManager {
	
	private String classroom;
	private SimpleDateFormat df = new SimpleDateFormat("YYYYMMdd");
	
	private String todayDate;
	private int i = 0;
	
	
	public DataManager(String classroom){
		this.classroom 	= classroom;
	}
	
	public String[] getData(Date date){
		this.todayDate = df.format(date);
		LinkedList<String> lessons = new LinkedList<>();
		try {
			Driver d = (Driver)Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
			Connection con = DriverManager.getConnection("jdbc:sqlserver://bakalari.ssinfis.local:1529;","sporic","sporic");
			DatabaseMetaData md = con.getMetaData();
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(
		"SELECT "+
				"(bakalari.dbo.s_rozvrh.HOD-3) as hodina, " +
				"bakalari.dbo.[nazvy_pr].ZKRATKA as predmet, " +
				"mistnost.ZKRATKA as mistnost, " +
				"tridy.ZKRATKA as trida, "+
				"bakalari.dbo.ucitele.ZKRATKA as ucitel "+
		"FROM " +
				"bakalari.dbo.[s_rozvrh], bakalari.dbo.[nazvy_pr], bakalari.dbo.mistnost, bakalari.dbo.tridy, bakalari.dbo.ucitele "+ 
		"WHERE "+ 
				"den = '"+ todayDate +"' and "+ 
				"s_rozvrh.KOD_TRID = tridy.KOD_TRID and "+
				"s_rozvrh.KOD_UCIT = ucitele.INTERN_KOD and "+
				"s_rozvrh.KOD_MIST  = mistnost.KOD_MIST and "+ 
				"s_rozvrh.KOD_PRED = bakalari.dbo.nazvy_pr.KOD_PRED and "+
				"mistnost.ZKRATKA = '"+ classroom +"' "+
		"ORDER BY HOD;");
			while(rs.next()){
				String hodina = rs.getString("hodina").trim();
				String predmet = rs.getString("predmet").trim();
				String mistnost = rs.getString("mistnost").trim();
				String trida = rs.getString("trida").trim();
				String ucitel = rs.getString("ucitel").trim();
				String lesson = hodina + "," + predmet + "," + mistnost + "," + trida + "," + ucitel;
					
					if (i>0 && lesson != null && lessons.size() > 0) {
						if(lesson.split(",")[0].equals(lessons.get(i-1).split(",")[0])){
							if (!lesson.split(",")[3].equals(lessons.get(i-1).split(",")[3])) {

								
								String[] tmp = lessons.get(i-1).split(",");
								tmp[3] = tmp[3] + "," + lesson.split(",")[3];
								
								String novy = "";
								
								for(int i = 0; i < tmp.length; i++){
									if(i == 0) novy = tmp[i];
									else novy = novy + "," + tmp[i];
								}
								
								lessons.removeLast();
								lessons.add(novy);
							}		
						}
						else{
							lessons.add(lesson);
							i++;	
						}
					}
					else{
						lessons.add(lesson);
						i++;
					}
			}
			
			String[] simple = new String[lessons.size()];
			for (int i = 0; i < lessons.size(); i++) {
				simple[i] = lessons.get(i);
				System.out.println(lessons.get(i));
			}
			
			return simple;
		} catch (InstantiationException | IllegalAccessException
				| ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}